

cc.Class({
    extends: cc.Component,

    properties: {
    //靶盘
    Target_plate:cc.Node,
    //准星
    sight_bead:cc.Prefab,
    //靶点
    target_spot:cc.Prefab,
    //输入
    text_input:cc.EditBox,
    //确定按钮
    enter:cc.Button,
    //得分
    scoreboard:cc.Label,
    //随机指定环数
    rand_ring:cc.Label,
    //音乐
    music:cc.AudioClip,
    },


    onLoad () {

        this.isonclicked_show = false;
        this.ismoveing = true;
        this.oldscale = 1;
        this.randnum = this.getrandom(1,9);
        this.rand_ring.string = "" + this.randnum;
        this.count = 0;
        this.score = 0;
        this.totalscore = 0;
        this.scoreboard.string = "score:" + this.totalscore;

        this.sight = cc.instantiate(this.sight_bead);
        this.node.addChild(this.sight);
        
        this.node.on("touchstart",this.Generation_start,this);
        this.node.on("touchmove",this.Generation_star,this);
        this.node.on("touchend",this.Generation,this);

     },

    //点击按钮放大
    enlarge:function(){

        this.isonclicked_show = true;
      this.Target_plate.scale = 2;
      this.scheduleOnce(function(){
       this.Target_plate.scale = this.oldscale;
       this.isonclicked_show = false;
      },1);

    },
    
    //点击显示距离输入和确定按钮
    onclicked_show:function(){

    this.isonclicked_show = true;
    this.enter.node.active = true;
    this.text_input.node.active = true;

    },

    getrandom:function(min,max){
     return min + parseInt(Math.random()*(max - min +1));
    },


    //确定显示缩放比例和距离
    enter_distance:function(){
        var scale_num = parseInt(this.text_input.string);
       
        if(scale_num < 50 || scale_num > 100 ||this.text_input.string == ""){
            alert("请重新输入，范围在50 - 100之间");
        }else{
            this.text_input.string = "" + scale_num;
            var num = 100 - (scale_num - 50);
            this.Target_plate.scale = num / 100;
            this.oldscale = this.Target_plate.scale;
            this.enter.node.active = false;
            this.text_input.node.active = false;
            this.isonclicked_show = false;
        }
        
    },

    text_input_method:function(Target){
  
     Target.string = this.text_input.string;
    },


    Generation_start:function(touch){
        if(this.isonclicked_show){        
            return;
        }
        var position = this.Target_plate.convertToNodeSpaceAR(touch.getLocation());

        this.scale_position = cc.pMult(position,this.Target_plate.scale);
        
        var ran1 = this.getrandom(-10,10);
        var ran2 = this.getrandom(-10,10);

        this.sight.setPosition(this.scale_position.x + ran1,this.scale_position.y + ran2);
        
        this.lastposition = cc.p(this.scale_position.x + ran1,this.scale_position.y + ran2);
    },
       
     Generation_star:function(touch){
        if(this.isonclicked_show){
            return;
        }
        
        var position = this.Target_plate.convertToNodeSpaceAR(touch.getLocation());
        this.scale_position = cc.pMult(position,this.Target_plate.scale);
        if(this.ismoveing){
            this.ismoveing = false;           
            this.schedule(this.random_position.bind(this),0.05);
        }
      
     },

     random_position:function(){
        this.sight.active = true;
       var ran1 = this.getrandom(-10,10);
       var ran2 = this.getrandom(-10,10);
       this.sight.setPosition(this.scale_position.x + ran1,this.scale_position.y + ran2);
       
       this.lastposition = cc.p(this.scale_position.x + ran1,this.scale_position.y + ran2);

     },

     Generation:function(touch){
        this.sight.active = false;
        this.ismoveing = true;   
        this.unscheduleAllCallbacks();     
        if(this.isonclicked_show){
            
            return;
        }
        cc.audioEngine.play(this.music,false); 
        this.count++;
       // var position = this.Target_plate.convertToNodeSpaceAR(touch.getLocation());
        var position =  this.lastposition;

        
        var scale_position = cc.pMult(position,this.Target_plate.scale);
        var distance = cc.pDistance(scale_position,cc.p(0,0));
        

        //如果击中靶子就计算得分否则就计算脱靶
        if(distance <= this.Target_plate.width / 2 * this.Target_plate.scale){
        
            var  target_spot = cc.instantiate(this.target_spot);
            this.Target_plate.addChild(target_spot);
            target_spot.setPosition(position);
            this.calculate_ringnum(distance);
            let temp = 10 - Math.abs(this.score - this.randnum);
            this.totalscore += temp;
            this.scoreboard.string = "score:" + this.totalscore;
            cc.myData.hit_num++;

        } else{

            this.score = 0;
            cc.myData.Miss_target++;

        }

        //随机数出现之前无法点击出现光标
        this.isonclicked_show = true;
        
     
      
        //随机出现数字
        this.scheduleOnce(function(){
            this.isonclicked_show = false; 
           
            this.randnum = this.getrandom(1,9);
            this.rand_ring.string = "" + this.randnum;

        },0.5);
        this.isonclicked_show = false;
        
        if(this.count >= 10){
       
            cc.myData.scorenum = this.totalscore;
            cc.director.loadScene("gameover");
                
        }
     },

     calculate_ringnum:function(distance){
       var range = this.Target_plate.width/ 2 / (9 + 0.5) * this.Target_plate.scale;
       var range1 = range / 2;
       
       
       if(distance >= 0 && distance <= range1){
           this.score = 10;
       }else if(distance > range1 && distance <= range + range1){
        this.score = 9;
        
       }else if(distance > range1 + range && distance <= range*2 + range1){
        this.score = 8;
       }else if(distance > range*2 + range1 && distance <= range*3 + range1){
        this.score = 7;
       }else if(distance > range*3 + range1 && distance <= range*4 + range1){
        this.score = 6;
       }else if(distance > range*4 + range1 && distance <= range*5 + range1){
        this.score = 5;
       }else if(distance > range*5 + range1 && distance <= range*6 + range1){
        this.score = 4;
       }else if(distance > range*6 + range1 && distance <= range*7 + range1){
        this.score = 3;
       }else if(distance > range*7 + range1 && distance <= range*8 + range1){
        this.score = 2;
       }else{
        this.score = 1;
       }

     },

    start () {

    },

    
    update (dt) {
      
       
    },
});
